package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.SkuItemDetailFeignClient;
import com.atguigu.gmall.item.to.SkuDetailTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @author lfy
 * @Description
 * @create 2023-08-18 14:33
 */
@Controller
public class ItemController {

    @Autowired
    SkuItemDetailFeignClient skuDetailFeignClient;

    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId,
                       Model model){

        //远程调用-查询商品的完整数据
        Result<SkuDetailTO> skuDetail = skuDetailFeignClient.getSkuDetail(skuId);

        if(skuDetail.getCode()!=200){ //收到对应的异常状态状态
            return "item/404";
        }

        SkuDetailTO skuDetailTO = skuDetail.getData();
        if(skuDetailTO!=null){
            model.addAttribute("categoryView",skuDetailTO.getCategoryView());
            model.addAttribute("skuInfo",skuDetailTO.getSkuInfo());
            model.addAttribute("price",skuDetailTO.getPrice());
            model.addAttribute("spuSaleAttrList",skuDetailTO.getSpuSaleAttrList());
            model.addAttribute("valuesSkuJson",skuDetailTO.getValuesSkuJson());
            return "item/index";
        }



        return "item/404";

    }
}
